package com.swgk.core.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.swgk.core.R;
import com.swgk.core.customview.DatePickerView;

import java.util.List;

public class CommonPickerDialog extends BaseBottomDialog {

    private DatePickerView pickerView;
    private TextView okView;
    private TextView cancelView;
    private PickerCallBack pickerCallBack;
    private String value;

    public CommonPickerDialog(Context context, DialogCallBack dialogCallBack) {
        super(context, dialogCallBack);
    }

    @Override
    protected int dialogLayout() {
        return R.layout.common_picker_dialog;
    }

    @Override
    protected void initView(DialogViewHolder holder,Bundle savedInstanceState) {
        pickerView = holder.itemView.findViewById(R.id.picker_dialog);
        okView = holder.itemView.findViewById(R.id.ok_view);
        cancelView = holder.itemView.findViewById(R.id.cancel_view);
        cancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
                if (pickerCallBack != null) {
                    pickerCallBack.result(value);
                }
            }
        });
        pickerView.setIsLoop(false);
        pickerView.setOnSelectListener(new DatePickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                value = text;
            }
        });
    }

    public void setValueList(List<String> valueList, String select) {
        pickerView.setData(valueList);
        if (!TextUtils.isEmpty(select)) {
            value = select;
            pickerView.setSelected(select);
        } else {
            if (valueList != null && valueList.size() > 0) {
                value = valueList.get(0);
            }
            pickerView.setSelected(0);
        }
    }

    public interface PickerCallBack {
        void result(String value);
    }

    public void setPickerCallBack(PickerCallBack pickerCallBack) {
        this.pickerCallBack = pickerCallBack;
    }
}
